<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Import text into wowdkper</title>
<link href="./script/admincss.css" rel="stylesheet" type="text/css">
</head>

<?php
/**
+-----------------------------------------------------------------------+
* @autor tonera <tonera at gmail.com>;
* @since 2006-8-6
* @version $Id: eqdkp2wowdkper.php,v 1.0 tonera$
* @description	将文本文件字符串数据导入到wowdkper
* It Imports text's data to wowdkper.2006-8-6
+-----------------------------------------------------------------------+
*/

set_time_limit(0);
error_reporting(0);

if(file_exists("./php/config.inc.php")) {
	$cfile	= "./php/config.inc.php";
}elseif(file_exists("../php/config.inc.php")) {
	$cfile	= "../php/config.inc.php";
}else {
	echo "请先安装wowdkper 1.3或以上版本.";
}
require_once $cfile;
include_once(CHINO_PATH . "/Chino.php");
require_once CHINO_MODPATH.'/config/config.inc.php';
$GLOBALS['adodb'] = Chino::getObject('adodb');
$GLOBALS['adodb']->debug	= false;
include_once CHINO_LIBPATH.'/db_mysql.inc.php';

//copy
$rs	= $GLOBALS['adodb']->execute("select * from ".TABLEHEAD."_copy");
while(!$rs->EOF) {
	$copyArray[$rs->fields['id']]	= $rs->fields['name'];
	$rs->MoveNext();
}
//eventtype
$rs	= $GLOBALS['adodb']->execute("select * from ".TABLEHEAD."_eventtype");
while(!$rs->EOF) {
	$etArray[$rs->fields['id']]	= $rs->fields['name'];
	$rs->MoveNext();
}
//class
$rs		= $GLOBALS['adodb']->execute("select * from ".TABLEHEAD."_work");
while(!$rs->EOF){
	$classarray[$rs->fields['id']]	= $rs->fields['name'];
	$rs->MoveNext();
}
//race
$rs		= $GLOBALS['adodb']->execute("select * from ".TABLEHEAD."_race");
while(!$rs->EOF){
	$racearray[$rs->fields['id']]	= $rs->fields['name'];
	$rs->MoveNext();
}

if(!empty($_POST['submit'])) {
	$etid	= (int)$_POST['etid'];
	$cid	= (int)$_POST['cid'];
	$raidtime	= date("Y-m-d");
	$regtime	= date("Y-m-d H:i:s");

	if(empty($_POST['textdata'])){
		exit("请将玩家字符串数据贴在输入框内。");
	}
	$con	= str_replace("\t"," ",$_POST['textdata']);
	$userarray	= explode("\n",$con);
	if(is_array($userarray)){
		//create event
			$sql	= "insert into ".TABLEHEAD."_event(name,etid,raidtime,cid) values('Oldtxt Data','$etid','$raidtime','$cid')";
			$GLOBALS['adodb']->execute($sql);
			$eid	= $GLOBALS['adodb']->Insert_ID();

		foreach($userarray as $key=>$val){
			$userinfo	= explode(' ',$val);
			$username	= trim($userinfo[0]);
			$workid		= array_search(trim($userinfo[2]),$classarray);
			$raceid		= array_search(trim($userinfo[3]),$racearray);
			$userdkp	= (float)trim($userinfo[1]);
			$level		= trim($userinfo[4]);
			if(empty($username)){
				continue;
			}
			//如果会员存在，取出id，更新分配表，如果副本用户存在，更新副本数据表
			//查找会员是否存在:如果不存在，写入，否则取id
			$sql	= "select id from ".TABLEHEAD."_user where name='$username'";
			$urs	= $GLOBALS['adodb']->SelectLimit($sql,1);
			$uid	= $urs->fields['id'];
			if(empty($uid)){
				//无法确认workid 和groupid
				$sql	= "insert into ".TABLEHEAD."_user(name,workid,raceid,stat,regtime,level,lastraidtime) values('$username','$workid','$raceid','1','$regtime','$level','$regtime')";
				$GLOBALS['adodb']->execute($sql);
				$uid	= $GLOBALS['adodb']->Insert_ID();
			}
			$sql	= "insert into ".TABLEHEAD."_itemdis(eid,uid,value,distime,stat,cid) values('$eid','$uid','$userdkp','$regtime','1','$cid')";
			$GLOBALS['adodb']->execute($sql);
			//如果用户此副本dkp中有记录，则更新。无则插入
			$sql	= "select uid from ".TABLEHEAD."_dkpvalues where uid='$uid' and copyid='$cid'";
			$ucrs	= $GLOBALS['adodb']->execute($sql);
			if(empty($ucrs->fields['uid'])){
				$sql	= "insert into ".TABLEHEAD."_dkpvalues(dkpvalue,uid,copyid,raidnum) values('$userdkp','$uid','$cid','1')";
			}else{
				$sql	= "update ".TABLEHEAD."_dkpvalues set dkpvalue=dkpvalue+$userdkp where uid='$uid' and copyid='$cid'";
			}
			$ers		= $GLOBALS['adodb']->execute($sql);
		}
		if($ers){
			echo "<font color=red>导入完成，请删除此文件.不要多次导入.[OK,finished. Please delete the file]</font>";
		}
	}else{
		exit("字符串格式不正确.");
	}
}


?>



<body>
<form name="form1" method="post" action="?">
<table width="100%" border="0">
  <tr class=""> 
    <th colspan="2">从文本数据导入到WOW-DKPer[Import text's data to wowdkper]</th>
  </tr>
  <tr> 
    <td width="30%">请选择导入的副本[Select copy]：</td>
    <td width="70%">
	<select name="cid">
	<?php 
	$copyArray	= is_array($copyArray)?$copyArray:array();
	foreach($copyArray as $key=>$val) {
		echo("<option value=$key>$val</option>");
	}
	?>
    </select></td>
  </tr>
  <tr> 
    <td>请选择事件类型[Select type of event]：</td>
    <td>
	<select name="etid">
	<?php 
	$etArray	= is_array($etArray)?$etArray:array();
	foreach($etArray as $key=>$val) {
		echo("<option value=$key>$val</option>");
	}
	?>
    </select></td>
  </tr>
  <tr> 
    <td colspan="2" align="center"><textarea name="textdata" rows="10" cols="80"></textarea></td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td><input name="submit" type="submit" id="submit"></td>
  </tr>
  <tr> 
    <td colspan=2>字符串示例[String example]:<br /><br />
tonera	2.0	圣骑士	人类	60<br />
huprist	9.0	战士	矮人	60<br />
萨奎恩野性	10.0	术士	侏儒	60<br />
xfire	11.0	法师	人类	60<br />
蛮锤	27.0	牧师	人类	60<br />
	</td>
  </tr>
</table>
</body>
</form>
</html>
